import 'dart:async';

import 'package:flutter_screenutil/screenutil.dart';

import 'package:shop/common/baseContext.dart';
import 'package:shop/common/shop_material.dart';
import 'package:shop/commonWidget/myImage.dart';
import 'package:shop/routers/Router.dart';
import 'package:shop/utils/ScreenUtils.dart';
import 'package:shop/utils/Storage.dart';

//引导页 初始化工具
class GuidePage extends StatefulWidget {
  GuidePage({Key key}) : super(key: key);
  @override
  _GuidePageState createState() => _GuidePageState();
}

class _GuidePageState extends State<GuidePage> {
  int _index = 3;
  Timer timer;
  Map results;
  @override
  void initState() {
    Map map = StorageUtil.getMap(MyUrl.startstartPage);
    if (map.containsKey("Data")) {
      results = map["Data"];
    }
    inini();
    super.initState();
    timer = Timer.periodic(Duration(seconds: 1), (timer) {
      if (_index > 0) {
        setState(() {
          _index--;
        });
      } else {
        timer.cancel();
        route.pushReplacementNamed(MyRoutes.tabs);
      }
    });
  }

  inini() async {
    //  Sqflite();
    Map res = await MyHttp()
        .post(MyUrl.startstartPage, needToken: false, cache: true);
    setState(() {
      results = res;
    });
  }

  @override
  Widget build(BuildContext context) {
    //初始化屏幕适配工具
    ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: false);
    BaseContext.baseContext = context;
    return Scaffold(
        body: Stack(
      children: <Widget>[
        ListView(
          children: <Widget>[
            results == null ? Text("") : MyImage.network(results["Img"]),
            results == null ? Text("") : MyImage.network(results["BtnImg"]),
          ],
        ),
        Positioned(
            top: 50.h,
            right: 40.w,
            child: FlatButton(
                onPressed: () {
                  timer.cancel();
                  route.pushReplacementNamed(MyRoutes.tabs);
                },
                child: Container(
                  color: Colors.grey.withOpacity(0.3),
                  child: Text("跳过$_index 秒"),
                ))),
      ],
    ));
  }
}
